<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 15, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "database.Database" );

/**
 * Static class for holding all processing & information 
 * of database in the system
 *
 * @static 
 */
class SysDatabase {
	
	/**
	 * Get the database instance of the system. 
	 *
	 * @return Database
	 */
	static function &getInstance() {
		static $instance;
		if (empty ( $instance )) {
			$siteConfig = SystemSite::getSiteConfig ();
			$options = array ('driver' => $siteConfig->dbtype, 'host' => $siteConfig->host, 'user' => $siteConfig->user, 'password' => $siteConfig->password, 'database' => $siteConfig->db, 'prefix' => $siteConfig->dbprefix );
			$instance = Database::getInstance ( $options );
			$instance->debug ( $siteConfig->debug );
		}
		return $instance;
	}
	
	/**
	 * This global static function loads the first row of a query into an object
	 *
	 * @access	public
	 * @return 	object
	 */
	static function loadObject($sql = '', $offset = 0) {
		if (! empty ( $sql )) {
			SysDatabase::setQuery ( $sql, $offset, 1);
		}
		$db = &SysDatabase::getInstance ();
		return $db->loadObject ();
	}
	
	/**
	 * Load a list of database objects
	 *
	 * If <var>key</var> is not empty then the returned array is indexed by the value
	 * the database key.  Returns <var>null</var> if the query fails.
	 *
	 * @access	public
	 * @param string The field name of a primary key
	 * @return array If <var>key</var> is empty as sequential list of returned records.
	 */
	static function loadObjectList($sql = '', $offset = 0, $limit = 0, $key = null) {
		if (! empty ( $sql )) {
			SysDatabase::setQuery ( $sql, $offset, $limit );
		}
		$db = &SysDatabase::getInstance ();
		return $db->loadObjectList ( $key );
	}
	
	/**
	 * Load a list of database rows (numeric column indexing)
	 *
	 * If <var>key</var> is not empty then the returned array is indexed by the value
	 * the database key.  Returns <var>null</var> if the query fails.
	 *
	 * @abstract
	 * @access public
	 * @param string The field name of a primary key
	 * @return array
	 */
	static function loadRowList($sql = '', $offset = 0, $limit = 0, $key = null) {
		if (! empty ( $sql )) {
			SysDatabase::setQuery ( $sql, $offset, $limit );
		}
		$db = &SysDatabase::getInstance ();
		return $db->loadRowList ( $key );
	}
	/**
	 * Sets the SQL query string for later execution.
	 *
	 * This static function replaces a string identifier <var>$prefix</var> with the
	 * string held is the <var>_table_prefix</var> class variable.
	 *
	 * @access public
	 * @param string The SQL query
	 * @param string The offset to start selection
	 * @param string The number of results to return
	 * @param string The common table prefix
	 */
	static function setQuery($sql, $offset = 0, $limit = 0, $prefix = '#__') {
		$db = &SysDatabase::getInstance ();
		return $db->setQuery ( $sql, $offset, $limit, $prefix );
	}
	
	/**
	 * Inserts a row into a table based on an objects properties
	 *
	 * @access	public
	 * @param	string	The name of the table
	 * @param	object	An object whose properties match table fields
	 * @param	string	The name of the primary key. If provided the object property is updated.
	 */
	static function insertObject($table, &$object, $keyName = NULL) {
		$db = &SysDatabase::getInstance ();
		return $db->insertObject ( $table, $object, $keyName );
	}
	
	/**
	 * Description
	 *
	 * @access public
	 * @param boolean $updateNulls
	 */
	static function updateObject($table, &$object, $keyName, $updateNulls = true) {
		$db = &SysDatabase::getInstance ();
		return $db->updateObject ( $table, $object, $keyName, $updateNulls );
	}
	
	/**
	 * This method loads the first field of the first row returned by the query.
	 *
	 * @access	public
	 * @return The value returned in the query or null if the query failed.
	 */
	static function loadResult($sql = null) {
		if ($sql) {
			SysDatabase::setQuery($sql, 0, 1);
		}
		$db = &SysDatabase::getInstance ();
		return $db->loadResult ();
	}
	
	/**
	 * Get the select result of a SELECT sql
	 *
	 * @param AbstractRubikDBObject $sqlOrFinder
	 * @param int $recordPerPage
	 * @param int $indexPage
	 * 
	 * @return SelectResult
	 */
	static function &getSelectResult($sqlOrFinder, $recordPerPage = null, $indexPage = 1, $prefix = "#__") {
		$db = &SysDatabase::getInstance ();
		return $db->getSelectResult ( $sqlOrFinder, $recordPerPage, $indexPage, $prefix );
	}
	
	/**
	 * Execute the query
	 *
	 * @param string $sql
	 * @return mixed A database resource if successful, FALSE if not.
	 */
	static function execute($sql) {
		$db = &SysDatabase::getInstance ();
		return $db->execute ( $sql );
	}
	
	/**
	 * Save a DTO into database
	 *
	 * @param AbstractDTO $dto
	 * @return AbstractDTO or false
	 */
	static function saveDTO(&$dto, $ignoreNull = true) {
		$db = &SysDatabase::getInstance ();
		return $db->saveDTO ( $dto, $ignoreNull );
	}
	
	/**
	 * Get data from database and fill it into input dto
	 * Input DTO must have key value.
	 *
	 * @param AbstractDTO $dto
	 * @return AbstractDTO or false
	 */
	static function fillData(&$dto) {
		$db = &SysDatabase::getInstance ();
		return $db->fillData ( $dto );
	}
}
?>
